<template>
  <view>
    <view>
      <!-- 选择收货地址的盒子 -->
      <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
        <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
      </view>

      <!-- 渲染收货信息的盒子 -->
      <view class="address-info-box" v-else @click="chooseAddress">
        <view class="row1">
          <view class="row1-left">
            <view class="username">
              收货人：
              <text>{{ address.userName }}</text>
            </view>
          </view>
          <view class="row1-right">
            <view class="phone">
              电话：
              <text>{{ address.telNumber }}</text>
            </view>
            <uni-icons type="arrowright" size="16"></uni-icons>
          </view>
        </view>
        <view class="row2">
          <view class="row2-left">收货地址：</view>
          <view class="row2-right">{{ addstr }}</view>
        </view>
      </view>
      <!-- 底部的边框线 -->
      <image src="/static/cart_border@2x.png" class="address-border"></image>
    </view>
  </view>
</template>

<script>
import { mapState, mapGetters, mapMutations } from 'vuex';
export default {
  name: 'my-address',
  data() {
    return {};
  },
  computed: {
    // 映射数据和计算属性
    ...mapState('m_user', ['address']),
    ...mapGetters('m_user', ['addstr'])
  },
  methods: {
    ...mapMutations('m_user', ['updateAddress']),
    async chooseAddress() {
      let [err, data] = await uni.chooseAddress().catch(e => console.log(e))
      if(err === null && data.errMsg === "chooseAddress:ok"){
        this.updateAddress(data)
      }
      // 用户没有授权获取收货地址
      if(err && (err.errMsg === "chooseAddress:fail auth deny" || err.errMsg === 'chooseAddress:fail authorize no response')){
        this.reAuth()
      }
      // this.reAuth();
    },
    async reAuth() {
      // 弹框提示
      let [err, res] = await uni.showModal({
        title: '检测到你未授权收货地址，是否授权？',
        confirmText: '确定',
        cancelText: '取消'
      });
      console.log(err, res);
      // 弹窗报错退出
      if (err) return;
      if (res.confirm) {
        // 点击确定
        uni.openSetting({
          // 授权结束，需要对授权的结果做进一步判断
          success(settingResult) {
            // 地址授权的值等于 true，提示用户 “授权成功”
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址');
            // 地址授权的值等于 false，提示用户 “您取消了地址授权”
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！');
          }
        });
      } else {
        // 点击取消
      }
    }
  }
};
</script>

<style lang="scss">
// 底部边框线的样式
.address-border {
  display: block;
  width: 100%;
  height: 5px;
}
// 选择收货地址的盒子
.address-choose-box {
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}
// 渲染收货信息的盒子
.address-info-box {
  font-size: 12px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 5px;

  // 第一行
  .row1 {
    display: flex;
    justify-content: space-between;

    .row1-right {
      display: flex;
      align-items: center;

      .phone {
        margin-right: 5px;
      }
    }
  }

  // 第二行
  .row2 {
    display: flex;
    align-items: center;
    margin-top: 10px;

    .row2-left {
      white-space: nowrap;
    }
  }
}
</style>
